<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Package">

  <!-- 
  	****************************************
  	* IMPORTS
	****************************************
  -->
  <PropertyGroup>
  	<MSBuildCommunityTasksPath>$(MSBuildProjectDirectory)\Tools\MSBuildCommunityTasks</MSBuildCommunityTasksPath>
  	<MSBuildUmbracoTasksPath>$(MSBuildProjectDirectory)\Tools\MSBuildUmbracoTasks</MSBuildUmbracoTasksPath>
  </PropertyGroup>
  
  <Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets" />
  <Import Project="$(MSBuildUmbracoTasksPath)\MSBuild.Umbraco.Tasks.Targets" />

  <!-- 
  	****************************************
  	* PROPERTIES
	****************************************
  -->
  <PropertyGroup>
	<PackageVersion>1.0</PackageVersion>
	<ProjectName>uQR</ProjectName>
  </PropertyGroup>

  <PropertyGroup>
	<RootDir>$(MSBuildProjectDirectory)</RootDir>
	<ProjectDir>$(RootDir)\Src\$(ProjectName)</ProjectDir>
	<BuildDir>$(RootDir)\Build</BuildDir>
	<PackageDir>$(RootDir)\Package</PackageDir>
  </PropertyGroup>
  
  <!-- 
  	****************************************
  	* TARGETS
	****************************************
  -->
  
  <!-- CLEAN -->
  <Target Name="Clean">
	<RemoveDir Directories="$(BuildDir)" Condition="Exists('$(BuildDir)')" />
  	<RemoveDir Directories="$(PackageDir)" Condition="Exists('$(PackageDir)')" />
	<MakeDir Directories="$(BuildDir)" />
  	<MakeDir Directories="$(PackageDir)" />
  </Target>
  
  <!-- UPDATE ASSEMBLEY VERSION -->
  <Target Name="UpdateAssemblyInfo" DependsOnTargets="Clean">
	<FileUpdate
        Encoding="ASCII"
        Files="$(ProjectDir)\Properties\AssemblyInfo.cs"
        Regex="AssemblyVersion\(&quot;.*&quot;\)\]" 
        ReplacementText="AssemblyVersion(&quot;$(PackageVersion).0.0&quot;)]" />
	<FileUpdate
        Encoding="ASCII"
        Files="$(ProjectDir)\Properties\AssemblyInfo.cs"
        Regex="AssemblyFileVersion\(&quot;.*&quot;\)\]" 
        ReplacementText="AssemblyFileVersion(&quot;$(PackageVersion).0.0&quot;)]" />
  </Target>
  
  <!-- COMPILE -->
  <Target Name="Compile" DependsOnTargets="UpdateAssemblyInfo">
	<MSBuild Projects="$(ProjectDir)\$(ProjectName).csproj" />
  </Target>
    
  <!-- PREPAIRE FILES --> 
  <Target Name="PrepairFiles" DependsOnTargets="Compile">
    <ItemGroup>
      <BinFile Include="$(ProjectDir)\Bin\$(ProjectName).dll" />
      <XsltFiles Include="$(ProjectDir)\Xslt\*.xslt" />
      <PluginFiles Include="$(ProjectDir)\Web\*.ashx" />
      <PackageFile Include="$(RootDir)\Package.xml" />
    </ItemGroup>
	<Copy SourceFiles="@(BinFile)" DestinationFolder="$(BuildDir)\bin" />
	<Copy SourceFiles="@(XsltFiles)" DestinationFolder="$(BuildDir)\xslt" />
	<Copy SourceFiles="@(PluginFiles)" DestinationFolder="$(BuildDir)\umbraco\plugins\uQR" />
	<Copy SourceFiles="@(PackageFile)" DestinationFolder="$(BuildDir)" />
  </Target> 
  
  <!-- MANIFEST -->
  <Target Name="Manifest" DependsOnTargets="PrepairFiles">
	<ItemGroup>
      <ManifestFiles Include="$(BuildDir)\**\*" Exclude="$(BuildDir)\Package.xml" />
    </ItemGroup>
	<ReadLinesFromFile File="$(RootDir)\Readme.txt"> 
		<Output TaskParameter="Lines" ItemName="Readme" /> 
	</ReadLinesFromFile>
	<ManifestUpdate ManifestFile="$(BuildDir)\Package.xml"
		WorkingDirectory="$(BuildDir)"
		PackageVersion="$(PackageVersion)"
		Readme="@(Readme->'%(Identity)', '%0a%0d')"
	    Files="@(ManifestFiles)" />
  </Target> 
    
  <!-- PACKAGE -->
  <Target Name="Package" DependsOnTargets="Manifest">
	<ItemGroup>
      <PackageFiles Include="$(BuildDir)\**\*.*" />
  	</ItemGroup>
	<Package ManifestFile="$(BuildDir)\Package.xml"
       WorkingDirectory="$(BuildDir)"
       OutputDirectory="$(PackageDir)"
	   Files="@(PackageFiles)" /> 
  </Target> 
  
</Project>